﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevelopmentKit.Cms.Domain;
using DevelopmentKit.Extensions.SharpArch.Repositorys;
using DevelopmentKit.Web.Controllers.ViewModels.ContentViewModels;
using SharpArch.NHibernate.Web.Mvc;

namespace DevelopmentKit.Web.Controllers
{
    /// <summary>
    /// 内容管理
    /// </summary>
    public class ContentMangerController : BaseController
    {
        private readonly IENSRepository<Content> _repository;
        private readonly IENSRepository<ViewTemplate>   _templateRepository;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="repository"></param>
        public ContentMangerController(IENSRepository<Content> repository, IENSRepository<ViewTemplate> templateRepository)
        {
            _repository = repository;
            _templateRepository = templateRepository;
        }
        /// <summary>
        /// 内容管理
        /// </summary> 
        /// <returns></returns>
        [HttpGet]
        public ActionResult Summary()
        { 
            return View(); 
       
        }

        /// <summary>
        /// 内容管理
        /// </summary> 
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index(int? id)
        {
            if(id.HasValue)
            {
                var entity = _repository.FindAll<Content>().FirstOrDefault(x => x.Id == id);
                return View(entity);
            } 
            return View(); 
       
        }
        /// <summary>
        /// 站点导航
        /// </summary>
        /// <returns></returns>
        public ActionResult Nav()
        {
            var model = _repository.FindAll<Content>().Where(x => x.Definition.IsRoot && x.Visible).ToList();
            return View(model);
        }
        /// <summary>
        /// 选择创建内容类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult SelectCreateDefinition(int id)
        {
            var entity = _repository.FindAll<Content>().FirstOrDefault(x => x.Id == id);
            if (null == entity)
            {
                return View("NoFound");
            }
            var definitions = entity.Definition.AllowChildrenDefinitions; 
            if (definitions.Count == 1)
            {
                return RedirectToAction("Create", new { id = id, definitionId = definitions.FirstOrDefault().Id });
            }
            return View(definitions);
        }
        /// <summary>
        /// 创建内容
        /// </summary>
        /// <returns></returns>
         [HttpGet]
        public ActionResult Create(int id,int definitionId)
        {
            var definition = _repository.FindAll<Definition>().FirstOrDefault(x => x.Id == definitionId);
            if (null == definition)
            {
                return View("NoFound");
            }
            ViewBag.Definition = definition; 

            return View(new CreateContentViewModel
                            {
                                Parent = id,
                                Definition = definitionId
                            });
        }

   

        /// <summary>
        /// 新建内容
        /// </summary>
        /// <param name="id"></param>
        /// <param name="definitionId"></param>
        /// <param name="command"> </param>
        /// <param name="model"></param>
        /// <returns></returns>
        [Transaction]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(int id, int definitionId, string command, CreateContentViewModel model)
        {
            var definition = _repository.FindAll<Definition>().FirstOrDefault(x => x.Id == definitionId);
            if (null == definition)
            {
                return View("NoFound");
            }
            var template = _templateRepository.Get(model.Template);
            var parent = _repository.Get(id);
            if (null == parent)
            {
                return View("NoFound");
            }
            try
            {
                if (ModelState.IsValid)
                {
                    var entity = new Content
                                     {
                                         Created = DateTime.Now,
                                         Modified = DateTime.Now,
                                         Name = model.Name, 
                                         Children = new List<Content>(),
                                         ContentDetails = new List<ContentDetail>(),
                                         Definition = definition, 
                                         Expires = null, 
                                         Root = parent.Root,
                                         Title = model.Title, 
                                         SortOrder = parent.Children.Count, 
                                         Parent = parent,
                                         Published = null,
                                         Saver = Current.Id,
                                         State = ContentState.Draft,
                                         Visible = true,
                                     }; 
                    if (command == "立刻发布")
                    {
                        entity.State = ContentState.Published;
                        entity.Published = DateTime.Now;
                    }
                    _repository.Save(entity);
                    return RedirectToAction("Index", "ContentManger", new {id});
                }
            }
            catch
            {
                ModelState.AddModelError("", "系统繁忙,请稍后再试");
            }
            ViewBag.Definition = definition; 
            return View(model);
        }

        /// <summary>
        /// 内容详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /ContentManger/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /ContentManger/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

    

        //
        // POST: /ContentManger/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
